//
//  CommentViewController.m
//  UISegmentControl_test
//
//  Created by Mac on 2020/12/1.
//  Copyright © 2020年 Lorisyy. All rights reserved.
//

#import "CommentViewController.h"
#import "../playground/CommentCellView.h"
static Comment *selectedRow;
static NSMutableArray *pos; // 图片的位置

@interface CommentViewController ()

@end

@implementation CommentViewController
+(void)setLabel:(UILabel*)Label{
    Label.numberOfLines = 0;
    Label.textColor = [UIColor blackColor];
    Label.backgroundColor = [UIColor redColor];
}
// 设置详细信息页面
+ (UIViewController *)initViewContollerWithCard:(Content *)c{
    UIViewController *ret = [[UIViewController alloc] init];
    ret.navigationItem.title = @"评论详情";
    // Avatar
    NSString *avatarLink = [c OwnAvatar];
    NSURL *url = [[NSURL alloc]initWithString:avatarLink];
    NSData *avaData = [[NSData alloc]initWithContentsOfURL:url];
    UIImageView* avatar = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 50, 50)];
    avatar.image = [[UIImage alloc]initWithData:avaData];
    
    
    // Name
    UILabel *Label1 = [[UILabel alloc]initWithFrame:
                       CGRectMake(40, 60, 300, 50)];
    [self setLabel:Label1];
    Label1.text = [c OwnName];
    
    
    // Time
    UILabel *Label2 = [[UILabel alloc]initWithFrame:
                       CGRectMake(40, 100, 300, 50)];
    [self setLabel:Label2];
    Label2.text = [NSString stringWithFormat:@"%ld",[c EditDate]];
    Label2.font  = [UIFont boldSystemFontOfSize:18];

    // context
    UILabel *Label3 = [[UILabel alloc]initWithFrame:
                       CGRectMake(40, 140, 300, 50)];
    [self setLabel:Label3];
    Label3.text = [c text];
    
    
    // tags
    UILabel *Label4 = [[UILabel alloc]initWithFrame:
                       CGRectMake(40, 160, 200, 100)];
    [self setLabel:Label4];
    Label4.text = [c Type];
    
    // 评论点赞收藏
    UIButton  *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    commentButton.frame = CGRectMake(151 , 130, 20, 20);
    [commentButton setImage:[UIImage imageNamed:@"comment.png"] forState:UIControlStateNormal];
    [ret.view addSubview:commentButton];
    
    UIButton  *starButton = [UIButton buttonWithType:UIButtonTypeCustom];
    starButton.frame = CGRectMake(1 , 180, 20, 20);
    [starButton setImage:[UIImage imageNamed:@"../assert/star.png"] forState:UIControlStateNormal];
    [starButton addTarget:self action:@selector(starButtonClicked:) forControlEvents:UIControlEventTouchDown];
    [ret.view addSubview:starButton];
    
    UIButton  *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    likeButton.frame = CGRectMake(1 , 130, 20, 20);
    [likeButton setImage:[UIImage imageNamed:@"like.png"] forState:UIControlStateNormal];
    [likeButton addTarget:self action:@selector(likeButtonClicked:) forControlEvents:UIControlEventTouchDown];
    [ret.view addSubview:likeButton];
    
    [ret.view addSubview:avatar];
    [ret.view addSubview:Label1];
    [ret.view addSubview:Label2];
    [ret.view addSubview:Label3];
    [ret.view addSubview:Label4];
    
    
    NSMutableArray *pos_t = [[NSMutableArray alloc] init];
    
    NSString *p1 = NSStringFromCGRect(CGRectMake(40, 320, 80, 80));
    NSString *p2 = NSStringFromCGRect(CGRectMake(130, 320, 80, 80));
    NSString *p3 = NSStringFromCGRect(CGRectMake(220, 320, 80, 80));
    NSString *p4 = NSStringFromCGRect(CGRectMake(40, 410, 80, 80));
    NSString *p5 = NSStringFromCGRect(CGRectMake(130, 410, 80, 80));
    
    [pos_t addObject:p1];
    [pos_t addObject:p2];
    [pos_t addObject:p3];
    [pos_t addObject:p4];
    [pos_t addObject:p5];
    
    // imgNum 确定图片个数
    AlbumContent * ac = [c album];
    NSInteger imgNum = [[ac images] count];
    // 图片
    for (NSInteger i = 0; i < imgNum; i++){
//        UIImage *img = [c getImgByIndex:i];
//        UIImageView *imgView = [[UIImageView alloc] init];
//        imgView.frame = CGRectFromString(pos_t[i]);
//        // NSLog(@"%d", (int)self.pos.count);
//        imgView.image = img;
//        [ret.view addSubview:imgView];
    }
    // 评论
    for (int i = 0; i < [c CommentNum]; i++){
        Comment *data;// 这里需要获取data
        CommentCellView *subComment = [[CommentCellView alloc] initWithData:data];
        [ret.view addSubview:subComment];
    }
    return ret;
}
// 设置页面信息
- (void)setInfo{
    // Name
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:
                          CGRectMake(40, 60, 300, 50)];
    nameLabel.numberOfLines = 0;
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.backgroundColor = [UIColor blackColor];
    nameLabel.text = [self.comment OwnName];
    [self.view addSubview:nameLabel];
    
    // Time
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:
                          CGRectMake(40, 60, 300, 50)];
    timeLabel.numberOfLines = 0;
    timeLabel.font  = [UIFont boldSystemFontOfSize:18];
    timeLabel.textColor = [UIColor blackColor];
    timeLabel.backgroundColor = [UIColor clearColor];
    timeLabel.text = [NSString stringWithFormat:@"%ld",[self.comment EditDate]];
    [self.view addSubview:timeLabel];
    
    // Context
    UILabel *experienceLabel = [[UILabel alloc]initWithFrame:
                                CGRectMake(40, 100, 300, 50)];
    experienceLabel.numberOfLines = 0;
    experienceLabel.textColor = [UIColor blackColor];
    experienceLabel.backgroundColor = [UIColor clearColor];
    experienceLabel.text = [self.comment text];
    [self.view addSubview:experienceLabel];
    
    AlbumContent * ac = [self.comment album];
    NSInteger imgNum = [[ac images] count];
    // 图片
    for (int i = 0; i < imgNum; i++){
        // UIImage *img = [self.comment getImgByIndex:i];
        // UIImageView *imgView = [[UIImageView alloc] init];
        // imgView.frame = CGRectFromString(pos[i]);
        // NSLog(@"%d", (int)self.pos.count);
        // imgView.image = img;
        // [self.view addSubview:imgView];
    }
    
    // Tag
    UILabel *Label3 = [[UILabel alloc]initWithFrame:
                       CGRectMake(40, 140, 300, 50)];
    Label3.numberOfLines = 0;
    Label3.textColor = [UIColor blackColor];
    Label3.backgroundColor = [UIColor redColor];
    Label3.text = [self.comment Type];
    [self.view addSubview:Label3];
    
    
}

- (void)setpos{
    NSString *p1 = NSStringFromCGRect(CGRectMake(40, 320, 80, 80));
    NSString *p2 = NSStringFromCGRect(CGRectMake(130, 320, 80, 80));
    NSString *p3 = NSStringFromCGRect(CGRectMake(220, 320, 80, 80));
    NSString *p4 = NSStringFromCGRect(CGRectMake(40, 410, 80, 80));
    NSString *p5 = NSStringFromCGRect(CGRectMake(130, 410, 80, 80));
    
    [pos addObject:p1];
    [pos addObject:p2];
    [pos addObject:p3];
    [pos addObject:p4];
    [pos addObject:p5];
}

- (void)popToLists:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setbackButton{
    UIButton *backbtn = [UIButton buttonWithType:UIButtonTypeSystem];
    backbtn.frame = CGRectMake(0, 0, 30, 30);
    backbtn.layer.borderColor = [UIColor grayColor].CGColor;
    backbtn.layer.borderWidth = 1.0;
    backbtn.layer.cornerRadius = 15.0;
    
    [backbtn setTitle:@"返回" forState:UIControlStateNormal];
    backbtn.titleLabel.font = [UIFont systemFontOfSize: 10.0];
    [backbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [backbtn addTarget:self action:@selector(popToLists:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backbtn];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.comment = selectedRow; // 获取发现页面中的详细信息
    //    self.card = selectedRow2; // 获取编辑页面中的打卡详细信息
    self.navigationItem.title = @"评论详情";
    [self setbackButton];
    [self setpos];
    [self setInfo];
}
-(void)setContent:(NSString*)contentID{
    
}
@end
